<?php

class indexComponents extends sfComponents
{
  public function executeMenu()
  {
    $this->form = new SearchForm();
    
    $request = sfContext::getInstance()->getRequest();
    $this->city = CityPeer::retrieveBySlug($request->getParameter('city_name'));
  }
  
  public function executeRightSide()
  {
    $this->cities = CityPeer::getAll(true);
    $this->categories = CategoryPeer::getAll();
    $this->city_id = null;
    $request = sfContext::getInstance()->getRequest();
    if($city = CityPeer::retrieveBySlug($request->getParameter('city_name')))
    {
      $this->city_id = $city->getId();
    }
  }
  
  public function executeRightSideSites()
  {
    $this->city_id = null;
    $this->city_name = 'all';
    
    $request = sfContext::getInstance()->getRequest();
    if($this->city = CityPeer::retrieveBySlug($request->getParameter('city_name')))
    {
      $this->city_id = $this->city->getId();
      $this->city_name = $this->city->getName();
    }
    $this->sites = SitePeer::getAll(true, $this->city_id);
    $nr_sites = count($this->sites);
    $i = 0;
    $this->sites1 = $this->sites2 = array();
    foreach($this->sites as $site)
    {
      if($i < $nr_sites / 2)
      {
        $this->sites1[] = $site;
      }
      else
      {
        $this->sites2[] = $site;
      }
      $i++;
    }
  }
  
  public function executeSearchKeywords()
  {
    $sql = "SELECT keyword FROM search_keyword WHERE results > 0 AND created_at > '".date("Y-m-d H:i:s", strtotime('-1 day'))."' ORDER BY created_at DESC LIMIT 100";
    $connection = Propel::getConnection();
    $statement = $connection->prepare($sql);
    $statement->execute();

    $this->search_keywords = array();
    while($resultset = $statement->fetch(PDO::FETCH_OBJ))
    {
        $this->search_keywords[] = $resultset->keyword;
    }
    
    $this->search_keywords = array_unique($this->search_keywords);
    $this->search_keywords = array_slice($this->search_keywords, 0, 20);
    
    $nr_keywords = count($this->search_keywords);
    $i = 0;
    $this->search_keywords1 = $this->search_keywords2 = array();
    foreach($this->search_keywords as $search_keyword)
    {
      if($i < $nr_keywords / 2)
      {
        $this->search_keywords1[] = $search_keyword;
      }
      else
      {
        $this->search_keywords2[] = $search_keyword;
      }
      $i++;
    }
  }
}
